package com.ailesson.order.service.impl;

import com.ailesson.common.except.GlobalErrorCodeConstants;
import com.ailesson.common.except.ServiceException;
import com.ailesson.order.entity.CouponUseEntity;
import com.ailesson.order.service.CouponUseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;

import com.ailesson.order.mapper.CouponInfoMapper;
import com.ailesson.order.entity.CouponInfoEntity;
import com.ailesson.order.service.CouponInfoService;
import com.ailesson.order.model.req.CouponInfoQueryReq;
import com.ailesson.order.model.req.CouponInfoReq;
import com.ailesson.order.model.reply.CouponInfoReply;
import com.ailesson.common.reply.PageResult;
import cn.hutool.core.collection.CollectionUtil;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@Service
public class CouponInfoServiceImpl extends ServiceImpl<CouponInfoMapper, CouponInfoEntity> implements CouponInfoService {

    @Autowired
    private CouponUseService couponUseService;
    @Override
    public PageResult queryPage(CouponInfoQueryReq params) {
       Page pageTmp = Page.of(params.getPageNo(),params.getPageSize());
       IPage<CouponInfoEntity> page = this.page(
               pageTmp,
               new LambdaQueryWrapper<CouponInfoEntity>()
                       .orderByDesc(CouponInfoEntity::getId)
       );
       List<CouponInfoReply> replyList = Collections.EMPTY_LIST;
       if (CollectionUtil.isNotEmpty(page.getRecords())){
           replyList = page.getRecords().stream().map(x -> {
               CouponInfoReply reply = new CouponInfoReply();
               BeanUtils.copyProperties(x, reply);
               return reply;
           }).collect(Collectors.toList());
       }
       return new PageResult<>(replyList,page.getTotal());
    }

    @Override
    public CouponInfoReply info(Long id) {
        CouponInfoEntity entity = this.getById(id);
        if (entity != null){
            CouponInfoReply reply = new CouponInfoReply();
            BeanUtils.copyProperties(entity, reply);
            return reply;
        }
        return null;
    }

    @Transactional(
            rollbackFor = {Exception.class}
    )
    @Override
    public void update(CouponInfoReq couponInfo) {
        if (couponInfo.getId() == null || couponInfo.getId() <= 0){
            throw new ServiceException(GlobalErrorCodeConstants.ID_EMPTY_ERROR);
        }
        CouponInfoEntity entity = this.getById(couponInfo.getId());
        if (entity == null){
            throw new ServiceException(GlobalErrorCodeConstants.NOT_EXISTS_ERROR);
        }
        BeanUtils.copyProperties(couponInfo, entity);
        this.updateById(entity);
    }

    @Transactional(
            rollbackFor = {Exception.class}
    )
    @Override
    public Long add(CouponInfoReq couponInfo) {
        couponInfo.setId(null);
        CouponInfoEntity entity = new CouponInfoEntity();
        BeanUtils.copyProperties(couponInfo, entity);
        this.save(entity);
        return entity.getId();
    }

    @Transactional(
            rollbackFor = {Exception.class}
    )
    @Override
    public void updateCouponInfoUseStatus(Long couponUseId, Long orderId) {
        CouponUseEntity couponUse = new CouponUseEntity();
        couponUse.setId(couponUseId);
        couponUse.setOrderId(orderId);
        couponUse.setCouponStatus("1");
        couponUse.setUsingTime(LocalDateTime.now());
        couponUseService.updateById(couponUse);
    }
}